package self.result;

import lombok.Data;
import self.enumerate.ResponseCode;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

@Data
public class R {

    //响应的状态码
    private Integer code;

    //消息提示
    private String message;

    //返回数据的封装
    private HashMap<String, Object> dataMap = new HashMap<>();

    private R(){}

    /**
     * 封装返回成功的结果
     * @return
     */
    public static R ok() {
        R r = new R();
        r.setCode(ResponseCode.SUCCESS.getCode());
        r.setMessage(ResponseCode.SUCCESS.getMessage());
        return r;
    }

    /**
     * 封装返回失败的结果
     * @return
     */
    public static R error() {
        R r = new R();
        r.setCode(ResponseCode.FAILURE.getCode());
        r.setMessage(ResponseCode.FAILURE.getMessage());
        return r;
    }

    /**
     * 失败返回一个枚举对象的情况
     * @param responseCode
     * @return
     */
    public static R error(ResponseCode responseCode) {
        R r = new R();
        r.setCode(responseCode.getCode());
        r.setMessage(responseCode.getMessage());
        return r;
    }

    /**
     * 直接返回了状态码和信息提示的情况
     * @param code
     * @param message
     * @return
     */
    public static R error(Integer code, String message) {
        R r = new R();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    /**
     * 直接返回一个键值对情况的封装
     * @param key
     * @param value
     * @return
     */
    public R data(String key, Object value) {
        this.dataMap.put(key, value);
        return this;
    }

    /**
     * 直接传递一个maps情况的封装
     * @param maps
     * @return
     */
    public R data(Map<String, Object> maps) {
        R r = new R();
        Set<Map.Entry<String, Object>> entries = maps.entrySet();
        Iterator<Map.Entry<String, Object>> it = entries.iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> next = it.next();
            String key = next.getKey();
            Object value = next.getValue();
            this.dataMap.put(key, value);
        }
        return this;
    }


}
